<template>
  <div class="login-box">
    <div class="title">登录界面</div>
    <el-form
      :model="LoginForm"
      status-icon
      :rules="rules"
      ref="LoginForm"
      label-width="45px"
      class="demo-LoginForm"
    >
    <div>{{info}}</div>
      <el-form-item label="账号" prop="username">
        <el-input
          type="text"
          v-model="LoginForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="LoginForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('LoginForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('LoginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapMutations} from "vuex"
import jwt from "jwt-decode"
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      LoginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
      info:""
    };
  },
  methods: {
    ...mapMutations("longinModule",['setUser']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("校验通过", this.LoginForm);
          //校验通过后开始请求接口
          let { username, password } = this.LoginForm;
          this.$api
            .getLogin({
              username,
              password,
            })
            .then((res) => {
              // console.log(res.data);
              if(res.data.status===200){
                console.log(jwt(res.data.data));//jwt 解码
                //登录成功后：存储用户信息
                let obj={
                  user:jwt(res.data.data).username,
                  token:res.data.data
                }
                this.setUser(obj)
                localStorage.setItem('user',JSON.stringify(obj))
                //跳转网页
                this.$router.push('/')
                //顶部区域显示用户信息

                //持久化（同级组件可以选择使用vuex）

              }else{
                // this.info = "账号或密码错误"
                this.$message.error("账号或密码错误")
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  width: 400px;
  height: 300px;
  padding: 20px;
  margin: 150px auto;
  border-radius: 10px;
  border: 1px solid #eee;
  background: #fff;
  text-align: center;
  .title {
    margin-bottom: 50px;
  }
}
</style>
